package com.shxy.service.impl;

import com.shxy.controller.ui.Pageable;
import com.shxy.dao.news.NewsCatagoryDao;
import com.shxy.dao.news.NewsContentDao;
import com.shxy.dao.news.NewsDao;
import com.shxy.dao.news.NewsInCatagoryDao;
import com.shxy.dao.security.UserDao;
import com.shxy.entity.news.NewsCatagory;
import com.shxy.entity.news.NewsContent;
import com.shxy.entity.news.NewsInCatagory;
import com.shxy.entity.news.NewsInfo;
import com.shxy.entity.security.CustomUser;
import com.shxy.service.INewsService;
import com.shxy.service.bean.NewsBean;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 新闻管理类
 */
@Service
public class NewsService implements INewsService {
    @Autowired
    private NewsDao newsDao;
    @Autowired
    private NewsContentDao contentDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private NewsCatagoryDao newsCatagoryDao;
    @Autowired
    private NewsInCatagoryDao newsInCatagoryDao;

    @Override
    public Optional<NewsInfo> getById(String id) {
        NewsInfo newsInfo = newsDao.findById(id).get();
        newsInfo.setContent(contentDao.getOne(newsInfo.getContentId()));
        return Optional.of(newsInfo);
    }

    @Override
    public Optional<NewsInfo> save(NewsInfo newsInfo, String content) {
        // 保存新闻内容
        NewsContent nc = contentDao.save(new NewsContent(content));
        newsInfo.setContentId(nc.getId());
        // 保存新闻本身
        NewsInfo news = newsDao.save(newsInfo);
        news.setContent(nc);

        return Optional.of(news);
    }

    @Override
    public Pageable<NewsBean> getList(int page, int size) {
        // 获取分页的新闻信息
        Page<NewsInfo> pn = newsDao.findAll(PageRequest.of(page - 1, size));
        List<NewsBean> nbs = newsConverter(pn.getContent());
        return new Pageable<>(pn.getTotalElements(), nbs, page, size);
    }

    @Override
    public NewsInCatagory saveNewsInCatagory(NewsInfo newsInfo, NewsCatagory nc) {
        // 1、从数据库中查找
        NewsInCatagory nic = newsInCatagoryDao.findByCatagoryIdAndNewsId(newsInfo.getId(), nc.getId());
        // 2、如果数据库中没有
        if(nic == null){
            nic = new NewsInCatagory();
            nic.setNewsId(newsInfo.getId());
            nic.setCatagoryId(nc.getId());
            nic = newsInCatagoryDao.save(nic);
        }
        return nic;
    }

    @Override
    public Pageable<NewsBean> getNewsOfCatagory(String catagoryId, int page, int size) {
        Page<NewsInCatagory> nics = newsInCatagoryDao.findAllByCatagoryId(catagoryId, PageRequest.of(page - 1, size));
        List<NewsInfo> infos = nics.getContent().stream().map(newsInCatagory -> newsDao.getOne(newsInCatagory.getNewsId())).collect(Collectors.toList());
        List<NewsBean> beans = newsConverter(infos);
        return new Pageable<>(nics.getTotalElements(), beans, page, size);
    }

    @Override
    public List<NewsCatagory> getCatagoryList() {
        return newsCatagoryDao.findAll();
    }

    /**
     * 新闻列表格式转换
     * @param infos
     * @return
     */
    private List<NewsBean> newsConverter(List<NewsInfo> infos) {
        return infos.stream().map(newsInfo -> {
            userDao.getOne(newsInfo.getAuthorId());
            NewsBean nb = new NewsBean();
            BeanUtils.copyProperties(newsInfo, nb);
            CustomUser author = userDao.getOne(newsInfo.getAuthorId());
            nb.setAuthor(author.getName());
            nb.setAuthorId(newsInfo.getId());
            return nb;
        }).collect(Collectors.toList());
    }
}
